 begintownscript;

 variables;

 int i,j,k,r1,choice;

 body;

 beginstate INIT_STATE;

	set_name(6,"Bastaricus");
	set_name(7,"Heavily armored soldier");
	set_name(8,"Captain Yarling");
	set_name(9,"Bearworth");
	set_name(10,"Lincoln");
	set_name(11,"Cerbos");
	set_name(12,"Gerry");
	set_name(13,"Father Indo");
	set_name(14,"Bastaricus");
	set_name(15,"Ursa");
	set_name(16,"Sarah");
	set_name(17,"Leonid");
	set_name(18,"Skyle");
	set_name(19,"Theodore");
	set_name(20,"Jacob");
	set_name(22,"Hendrickson");
	set_name(23,"Stanley");
	set_name(25,"Kate");
	set_name(26,"Sigmund");
	set_name(28,"Sally");
	set_name(41,"Mayor Lyrth");

	set_crime_tolerance(1);

	if (get_flag(1,1) == 2) {
		erase_char(7);
		erase_char(6);
}

	if (get_flag(200,2) == 0) {
		set_flag(200,2,50);
		force_instant_terrain_redraw();
		reset_dialog();
		add_dialog_str(0,"You arrive at the largest settlement in Faerengrove, Grimson. It is the city where the bureaucracy takes place and which distributes food to the citizens of Faerengrove.",0);
		add_dialog_str(1,"The city itself, however, is not impressive. The buildings are mostly made of wood and are small. The streets are quiet; not many people wander on them.",0);
		add_dialog_str(2,"You might want to have a word with the mayor of Grimson, for he is the Chief Empire Liaison for Faerengrove.",0);
		add_dialog_choice(0,"OK.");
		run_dialog(1);
}

	if (get_flag(1,26) > 2 && get_flag(9,9) != 1)
		erase_char(22);

	if (get_flag(1,16) == 5) {
		set_terrain(22,17,0);
		set_terrain(22,19,126);
		set_terrain(22,20,246);
		activate_hidden_group(2);
		set_character_pose(40,2);
}



break;

 beginstate START_STATE;

	if (get_ran(1,0,100) < 6)
		text_bubble_on_char(23,"Whee!");
	if (get_ran(1,0,100) < 6 && get_flag(1,16) != 5)
		text_bubble_on_char(25,"Sigh.");
	if (get_ran(1,0,100) < 6 && get_flag(1,16) == 5)
		text_bubble_on_char(25,"Hold that pose!");
	if (get_ran(1,0,100) < 6)
		text_bubble_on_char(25,"*chip!*");
	if (get_ran(1,0,100) < 5)
		text_bubble_on_char(10,"Work faster Cerboy!");
	if (get_ran(1,0,100) < 6)
		text_bubble_on_char(10,"Rooms available!");
	if (get_ran(1,0,100) < 6)
		text_bubble_on_char(8,"Hmm.");
	if (get_ran(1,0,100) < 6)
		text_bubble_on_char(8,"Why now?");
	if (get_ran(1,0,100) < 6)
		text_bubble_on_char(26,"*Cough!*");
	if (get_ran(1,0,100) < 6)
		text_bubble_on_char(9,"Most peculiar");
	if (get_ran(1,0,100) < 6)
		text_bubble_on_char(9,"*Hawk!*");
	if (get_ran(1,0,100) < 6 && get_flag(1,13) == 1)
		text_bubble_on_char(29,"Zzzz.");
	if (get_ran(1,0,100) < 6)
		text_bubble_on_char(36,"Zzz!");
	if (get_ran(1,0,100) < 6)
		text_bubble_on_char(21,"Bark");
	if (get_ran(1,0,100) < 6)
		text_bubble_on_char(21,"Bark! Bark!");


	if (get_flag(1,2) == 1) {
		approach_waypoint(9,0,3);
		set_terrain(40,44,52);
		relocate_character(18,43,48);
		set_mobility(18,0);
}

	if (dist_to_waypoint(9,0) == 0) {
		set_flag(1,2,2); }

	if (get_flag(1,2) == 2) {
		approach_waypoint(9,1,3);
}

	if (dist_to_waypoint(9,1) == 0) {
		set_flag(1,2,3); 
}

	if (get_flag(1,2) == 3) {
		approach_waypoint(9,2,0);
}
	if (dist_to_waypoint(9,2) == 0) {
		change_blocked(39,50,1);
		change_blocked(39,51,1);
		change_blocked(38,51,1);
		set_flag(1,2,0);
		set_creature_memory_cell(9,0,2);
		set_character_facing(9,0);
}	

	if (group_dist_to_loc(0,40,51) <= 1 && dist_to_waypoint(9,2) == 0) {
		reset_dialog();
		add_dialog_str(0,"Bearworth stops at one of the shelves and grabs a book. You can't make out the title, but you make out the word. _demon_. Bearworth flips to a certain page and compares the amulet you gave him to a drawing on the book. He doesn't look happy.",0);
		add_dialog_str(1,"_Thank the gods to Skyle's inexplicable lust for books. Otherwise we might not know what we know now._ He throws the amulet back to you. _It's the amulet of Kaejil Nyh'yltak, one of the leading Haakais of hell._",0);
		add_dialog_str(2,"_We're in trouble, if he has been somehow summoned to our realm. I need to inform the mayor, and possibly convince him to do something for you. Meanwhile you should speak to Bastaricus._",0);
		add_dialog_str(3,"_He is in this library and knows something about demons. Now, I'm off._ The wizard paces outside and leaves you standing.",0);
		add_dialog_choice(0,"OK.");
		run_dialog(1);
		set_flag(1,3,1);
		set_flag(0,29,5);
		relocate_character(9,0,0);
		erase_char(9);
		relocate_character(9,0,0);
}

	if ((get_flag(101,0) > 0) || ((get_flag(17,17) >= 2) && (get_flag(17,9) == 1) && (get_flag(175,5) == 0))) {
		erase_char(20);
		erase_char(21);
		activate_hidden_group(7);
}
	if (get_flag(40,0) == 1)
		set_flag(40,0,2);
break;

 beginstate EXIT_STATE;

break;


 beginstate 10;
	if (get_flag(1,1) == 1 && char_on_loc(44,56)) {
	force_view_center(43,56);
	pause(3);
	relocate_character(7,43,56);
	relocate_character(23,41,55);
	relocate_character(18,42,55);
	set_character_facing(23,4);
	set_character_facing(7,7);
	set_character_facing(0,3);
	set_character_facing(1,4);
	set_character_facing(2,4);
	set_character_facing(3,4);
	activate_hidden_group(1);
	pause(5);
	text_bubble_on_char(7,"Mmnm.");
force_instant_terrain_redraw();
	pause(5);
	set_character_facing(6,3);
	text_bubble_on_char(7,"");
	text_bubble_on_char(6,"What the? Oops!");
force_instant_terrain_redraw();
	pause(5);
	play_sound(104);
	pause(3);
	put_boom_on_char(7,1,0);
	run_animation_sound(109);
force_instant_terrain_redraw();
	pause(5);
	erase_char(7);
force_instant_terrain_redraw();
	text_bubble_on_char(6,"");		
	text_bubble_on_char(23,"Oh my god!");
force_instant_terrain_redraw();
	pause(11);
	text_bubble_on_char(23,"");
	text_bubble_on_char(23,"They killed Kenny!");
force_instant_terrain_redraw();
	pause(11);
	text_bubble_on_char(23,"");
	text_bubble_on_char(18,"You! Bastaricus!");
force_instant_terrain_redraw();
	pause(11);
	text_bubble_on_char(18,"");
	text_bubble_on_char(18,"Get out! Now!");
force_instant_terrain_redraw();
	pause(11);
	text_bubble_on_char(6,"But, I... I...");
force_instant_terrain_redraw();
	pause(10);
	text_bubble_on_char(6,"");
	relocate_character(6,char_loc_x(6) - 1,char_loc_y(6));
	play_sound(49);
force_instant_terrain_redraw();
	pause(2);
	relocate_character(6,char_loc_x(6) - 1,char_loc_y(6));
	play_sound(49);
force_instant_terrain_redraw();
	pause(2);	
	relocate_character(6,char_loc_x(6) - 1,char_loc_y(6));
	play_sound(49);
force_instant_terrain_redraw();
	pause(2);	
	relocate_character(6,char_loc_x(6) - 1,char_loc_y(6));
	play_sound(49);
force_instant_terrain_redraw();
	pause(2);
	relocate_character(6,char_loc_x(6) - 1,char_loc_y(6));
	play_sound(49);
force_instant_terrain_redraw();
	pause(2);
	relocate_character(6,char_loc_x(6) - 1,char_loc_y(6));
	play_sound(49);
force_instant_terrain_redraw();
	pause(2);
	set_character_facing(6,0);
	relocate_character(6,char_loc_x(6),char_loc_y(6) - 1);
	play_sound(49);
force_instant_terrain_redraw();
	pause(2);
	relocate_character(6,char_loc_x(6),char_loc_y(6) - 1);
	play_sound(49);
force_instant_terrain_redraw();
	pause(2);
	relocate_character(6,char_loc_x(6),char_loc_y(6) - 1);
	play_sound(49);
force_instant_terrain_redraw();
	pause(2);
	relocate_character(6,char_loc_x(6),char_loc_y(6) - 1);
	play_sound(49);
force_instant_terrain_redraw();
	pause(2);
	relocate_character(6,char_loc_x(6),char_loc_y(6) - 1);
	play_sound(49);
force_instant_terrain_redraw();
	pause(2);
	relocate_character(6,char_loc_x(6),char_loc_y(6) - 1);
	play_sound(49);
force_instant_terrain_redraw();
	pause(2);
	relocate_character(6,char_loc_x(6),char_loc_y(6) - 1);
	play_sound(49);
force_instant_terrain_redraw();
	pause(2);
	relocate_character(6,43,42);
	play_sound(49);
force_instant_terrain_redraw();
	pause(2);
	set_flag(1,1,2);
	erase_char(6);
	activate_hidden_group(5);
	set_mobility(18,1);
}
 break; 

 beginstate 11;
	if ((get_flag(1,1) != 2) && (get_flag(202,1) < 4)) {
		message_dialog("The guard blocks your way with his sword. _Mmnm mm!_ He says and gestures you to move away from the door.","");
		block_entry(1);
}
 break;

 beginstate 12;
	if (get_flag(1,17) == 0) {
		message_dialog("The Prancing Gremlin is like the other inns you've visited, if not a bit more quiet. One thing catches your attention. There is a gremlin, most likely working, in the inn.","Gremlins are not known for their love for humans. How the innkeeper got a gremlin working here is a mystery.");
		set_flag(1,17,1);
}

 break;

 beginstate 14;
	if (get_flag(1,14) == 0) {
		force_view_center(15,37);
		force_instant_terrain_redraw();
		begin_talk_mode(149);
}
 break;

 beginstate 15;
	if (get_flag(1,18) == 0) {
		message_dialog("This room looks like a trophy room. There are animal skins on the tables and animal heads on the walls. The trophies are in flawless condition but the room itself is dirty, as if the owner cared only for the trophies.","You hear movement in the room ahead. The owner must be home.");
		set_flag(1,18,1);
}
 break;

 beginstate 16;
	if (get_flag(1,19) == 0) {
		message_dialog("All the necessary paperwork and bureaucracy that isn't sent forward to the Empire bureaus is kept in this storage room. The air is dusty and there's hardly any lighting.","");
		set_flag(1,19,1);
}
 break;

 beginstate 17;
	message_dialog("These scrolls contain the dates and other figures involving the export of metal from Faerengrove. You take a quick glimpse on one of the papers and then put it back, confident that there is nothing worth learning here.","");

 break;
 beginstate 18;
	if (get_flag(1,20) == 0) {
		reset_dialog();
		add_dialog_str(0,"Someone has made it their business to build a fence, separating them from the rest of the town. There is also a small patch of wheat growing next to the house. There is sharp barking coming from the building.",0);
		add_dialog_str(1,"After a while of barking you hear a voice. _Ye ain't welcome! So remove yer hide from me lawn!_ So much for a warm welcome.",1);
		add_dialog_choice(0,"OK.");
		run_dialog(1);
		set_flag(1,20,1);
}
 break;

 beginstate 19;
	if (get_flag(1,21) == 0) {
		message_dialog("Now here's something you did not expect to find, an impressive library. Numerous shelves of books open up before you, by any standards, this is a large library.","");
		set_flag(1,21,1);
}
 break;

 beginstate 20;
	if (get_flag(1,28) == 0) {
		message_dialog("Looking at the row of books reveals one thing; the library may be large in size but the books are of low quality. You notice trashy novels and serious tomes on magic side by side. Some of them are just scrolls with writing on them","It's as if the keeper of the library just wanted to pile massive amounts of books together, without assorting them in any way, or discarding the petty volumes.");
		set_flag(1,28,1);
}
	if (get_flag(1,28) == 1)
		message_dialog("You find nothing of interest, just garbage on covers.","");

 break;

 beginstate 21;
	message_dialog("These shelves are still waiting to be filled with who knows what kind of books.","");
 break;

 beginstate 22;
	message_dialog("This bookcase is different from the other ones. It's organized and focused on one topic only, demons. There are all sorts of books ranging from demon behavior to the hierarchical order of demons.","You doubt that these shelves were organized by the librarian. It must have been someone else fixated with demons.");
 break;

 beginstate 23;
	if (get_flag(1,24) == 0) {
		message_dialog("Strange religions or cults are not uncommon in the more remote areas. This small temple is most probably the only temple, serving this deity. The temple has been built in white marble, but somehow it looks gloomy.","");
	set_flag(1,24,1);
}
 break;

 beginstate 24;
	message_dialog("You move the curtain slightly. It covers a painting or a beautiful noble woman. The painting has been made by a skilled artist. You cannot comprehend why it has been covered.","The plaque reads: _Lady Miriam Lyrth._");
	set_flag(1,27,1);
 break;

 beginstate 25;
	if (get_flag(1,25) == 0 && get_flag(202,1) != 4) {
		reset_dialog();
		add_dialog_str(0,"You enter the mayor's office. Bearworth is already there, he gives you a quick nod. The mayor is in his late twenties. _Greetings I am Mayor Lyrth, chief Empire Liaison for Faerengrove. Bearworth here has explained the situation to me.",0);
		add_dialog_str(1,"_He thinks the goblins and the undead that have been plaguing us are somehow connected, and that the connection lies within, demons. I have for you here..._ The mayor hands you a piece of paper.",0);
		add_dialog_str(2,"_That paper authorizes you as temporary special agents of the Empire. It entitles you full cooperation of any Empire soldiers, and the authorization to end the undead threat, and the cause of it, anyway you see fit._",0);
		add_dialog_str(3,"_If you have any questions, please sit down._",0);
		add_dialog_choice(0,"OK.");
		run_dialog(1);
		set_flag(1,25,1);
		toggle_quest(8,1);
		change_spec_item(5,1);
}
	if (get_flag(1,25) == 0 && get_flag(202,1) == 4) {
		reset_dialog();
		add_dialog_str(0,"You enter the mayor's office. The mayor is sitting at his desk. He is in his late twenties. _Greetings I am Mayor Lyrth, chief Empire Liaison for Faerengrove. I understand you have some information concerning the vampire._",0);
		add_dialog_str(1,"_I hope this is important. Ah, but where are my manners? Please sit down and tell me what you know!_",0);
		add_dialog_choice(0,"OK.");
		run_dialog(1);
		set_flag(1,25,1);
}
 break;

 beginstate 26;
	if (get_flag(1,26) != 4)
		message_dialog("You inspect the box in front of, it is clearly the work of a master carpenter and locksmith. There is no way you could get it open without the key.","");
 break;
